import pandas as pd

df = pd.read_csv('static/data/house_info_pre.csv')


def bar_data():
    df_bar = df[df['house_type'] != '无'].groupby('house_type')['title'].count().sort_values(ascending=False).reset_index().head(10)
    label = [i[0] for i in df_bar.values]
    data = [i[1] for i in df_bar.values]
    return {
        'label': label,
        'data': data
    }


def pie_data():
    df_pie = df[df['floor'] != '无'].groupby('floor')['title'].count().sort_values(ascending=False).reset_index()
    data = [{'name': i[0], 'value': i[1]} for i in df_pie.values]
    return {
        'data': data
    }


def boxplot_data():
    df_box = df[df['floor'] != '无'][['floor', 'price']].groupby('floor')
    label = [i[0] for i in df_box]
    data = [[j[1] for j in i[1].values] for i in df_box]
    return {
        'label': label,
        'data': data
    }


def word_cloud_data():
    words = [j for i in df[df['tags'] != '无']['tags'].values for j in str(i).split('|')]
    df_words = pd.DataFrame({'word': words})
    df_words_count = df_words['word'].value_counts().reset_index()
    data = [{'name': i[0], 'value': i[1]} for i in df_words_count.values]
    return {
        'data': data
    }


def geo_data():
    df_region = df[df['address'] != '无']['address'].apply(lambda x: x.split('-')[0])
    df_map = df_region.value_counts().reset_index()
    data = [{'name': i[0], 'value': i[1]} for i in df_map.values]
    return {
        'data': data
    }


def heatmap_data():
    face_list = df[df['face'] != '无']['face'].unique().tolist()
    price_list = [f'{2000 * i}-{2000 * (i + 1)}' for i in range(0, 10)]
    data = [
        [
            j,
            i,
            len(df[(df['price'] > 2000 * i) & (df['price'] <= 2000 * (i + 1)) & (df['face'] == face_list[j])])]
        for i in range(0, 10)
        for j in range(len(face_list))
    ]
    return {
        'face': face_list,
        'price': price_list,
        'data': data
    }
